home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue52 / HTML / Code / AppServer / mleObjectCache.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-10-24  |  1.6 KB  |  74 lines

  1. unit mleObjectCache;
  2.  
  3. interface
  4.  
  5. uses
  6.   Classes, DB, DBTables, SysUtils, dpoBase, mlePropertyInfo;
  7.  
  8. type
  9.   EInvalidObjectReference = Exception;
  10.  
  11.   TObjectCache = class(TStringList)
  12.   public
  13.     constructor Create;
  14.     destructor Destroy; override;
  15.     procedure Clear; override;
  16.     procedure GetPropertyInfo(aPropertyReference: string; aPropertyInfo: TPropertyInfo);
  17.     function GetPropertyValue(aPropertyName: string): string;
  18.   end;
  19.  
  20. implementation
  21.  
  22. { TObjectCache }
  23.  
  24. procedure TObjectCache.Clear;
  25. var
  26.   I: Integer;
  27. begin
  28.   for I := 0 to Count - 1 do
  29.     TDataObject(Objects[I]).Free;
  30.   inherited Clear;
  31. end;
  32.  
  33. constructor TObjectCache.Create;
  34. begin
  35.   inherited;
  36.   Sorted := True;
  37.   Duplicates := dupError;
  38. end;
  39.  
  40. destructor TObjectCache.Destroy;
  41. begin
  42.   Clear;
  43.   inherited;
  44. end;
  45.  
  46. procedure TObjectCache.GetPropertyInfo(aPropertyReference: string; aPropertyInfo: TPropertyInfo);
  47. var
  48.   ObjectName: string;
  49.   I, P: Integer;
  50. begin
  51.   P := Pos('.', aPropertyReference);
  52.   ObjectName := Copy(aPropertyReference, 1, P - 1);
  53.   I := IndexOf(ObjectName);
  54.   if I = -1 then
  55.     raise EInvalidObjectReference.CreateFmt('Invalid object name: "%s"', [ObjectName]);
  56.   aPropertyInfo.Instance := TDataObject(Objects[I]);
  57.   aPropertyInfo.PropertyName := Copy(aPropertyReference, P + 1, Length(aPropertyReference) - P);
  58. end;
  59.  
  60. function TObjectCache.GetPropertyValue(aPropertyName: string): string;
  61. var
  62.   PropertyInfo: TPropertyInfo;
  63. begin
  64.   PropertyInfo := TPropertyInfo.Create;
  65.   try
  66.     GetPropertyInfo(aPropertyName, PropertyInfo);
  67.     Result := PropertyInfo.AsString;
  68.   finally
  69.     PropertyInfo.Free;
  70.   end;
  71. end;
  72.  
  73. end.
  74.